package TestDemo;

//子类与父类同名的变量
class Base{
    public int a = 1;
    public int b = 2;

    public void methodA(){
        System.out.println("Base::methodA()");
    }
    public void methodB(){
        System.out.println("Base::methodB()");
    }
}
class Derived extends Base {
    public int a = 3;
    public int d = 4;

    public void methodB(){
        System.out.println("Derived::methodB()");
    }
    public void methodA(int Val){
        System.out.println("Base::methodA()"+Val);
    }

    public void test(){
//        变量名相同优先访问自己的《就近原则》
        System.out.println(this.a);
        System.out.println(this.b);
        System.out.println(this.d);
        System.out.println("访问父类的a ："+super.a);
        methodA();
        methodA(10);
        methodB();
//        methodB();
    }
}
//总结 子类父类方法成员名称相同，优先调用自己的，若想调用父类则用super
public class Test {
    public static void main(String[] args) {
     Derived derived = new Derived();
     derived.test();
    }
}
